import { createApp } from 'vue'

import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css'

import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'



// 根据前端的跨域方式做调整 /a/b : /api/a/b => /a/b
// axios.defaults.baseURL = 'https://www.easy-mock.com/mock/5dc7afee2b69d9223b633cbb/mimall';
axios.defaults.baseURL = 'http://127.0.0.1:3999/';
axios.defaults.timeout = 8000; 
// 根据环境变量获取不同的请求地址
// axios.defaults.baseURL = env.baseURL;
// 接口错误拦截
axios.interceptors.response.use(function(response){
  let res = response.data;
  if(res.status == 200){
    return res.data;
  }else if(res.status>=300){
    return res;
  }else if(res.status == 10){
    window.location.href = '/#/login';
    return Promise.reject(res);
  }else{
    Message.warning(res.msg);
    return Promise.reject(res);
  }
},(error)=>{
  let res = error.response;
  Message.error(res.data.message);
  return Promise.reject(error);
});

axios.interceptors.request.use(function(config){

  let token = localStorage.getItem('token');
  if(token!=null){
    config.headers['Authorization'] = token;
    console.log(token)
  }

  return config;
});

createApp(App).use(store).use(router).use(ElementPlus).mount('#app')
